package cn.hnucm.orderservice.controller;


import cn.hnucm.orderservice.entity.TbCapital;
import cn.hnucm.orderservice.service.TbCapitalService;
import cn.hnucm.orderservice.utils.MyLog;
import cn.hnucm.orderservice.utils.Result;
import cn.hnucm.orderservice.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账单明细 前端控制器
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-06
 */
@Api(tags = "账单明细")
@RestController
@RequestMapping("/orderservice/tbCapital")
public class TbCapitalController {

    @Autowired
    private TbCapitalService tbCapitalService;

    /**
     * 通过用户的Id查询其所有的 支出 账单
     * @param userId
     * @return
     */
    @ApiOperation(value = "通过用户ID查询支出账单")
    @GetMapping("/getUserFromCapital/{userId}")
    @ResponseBody
    @MyLog("通过用户ID查询支出账单")
    public Result getUserFromCapital(@PathVariable("userId") Integer userId) {
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        System.out.println("lalalal");
        List<TbCapital> userFromCapital = tbCapitalService.getUserFromCapital(userId);
        if (userFromCapital == null){
            return Result.error(ResultCode.SERVER_ERROR);
        } else if (userFromCapital.isEmpty()){
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userFromCapital", userFromCapital);
    }

    /**
     * 通过传入用户端的Id查询其所有的 收入 账单
     * @param userId
     * @return
     */
    @ApiOperation(value = "通过用户ID查询收入账单")
    @GetMapping("/getUserToCapital/{userId}")
    @ResponseBody
    @MyLog("通过传入用户端的Id查询其所有的 收入 账单")
    public Result getUserToCapital(@PathVariable("userId") Integer userId) {
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbCapital> userToCapital = tbCapitalService.getUserToCapital(userId);
        if (userToCapital == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        } else if (userToCapital.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userToCapital", userToCapital);
    }

    /**
     * 通过用户Id查询用户的所有账单
     * @param userId
     * @return
     */
    @ApiOperation(value = "通过用户ID查询所有账单")
    @GetMapping("/getUserAllCapital/{userId}")
    @ResponseBody
    @MyLog("通过用户Id查询用户的所有账单")
    public Result getUserAllCapital(@PathVariable("userId") Integer userId) {
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbCapital> userAllCapital = tbCapitalService.getUserAllCapital(userId);
        if (userAllCapital == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }else if (userAllCapital.isEmpty()){
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userAllCapital", userAllCapital);
    }

    /**
     * 通过用户Id和时间段查询用户某时间段的账单
     *
     * @param userId
     * @param beginTime
     * @param endTime
     * @return
     */
    @ApiOperation(value = "通过用户Id和时间段查询用户某时间段的账单")
    @GetMapping("/getUserSometiomeCapital/{userId}/{beginTime}/{endTime}")
    @ResponseBody
    @MyLog("通过用户Id和时间段查询用户某时间段的账单")
    public Result getUserSometiomeCapital(@PathVariable("userId") Integer userId,
                                          @PathVariable("beginTime") String beginTime,
                                          @PathVariable("endTime") String endTime) {
        if (userId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        if (beginTime == null) {
            beginTime = "0000-00-00";
        }
        if (endTime == null) {
            endTime = "9999-12-31";
        }
        List<TbCapital> userSometiomeCapital = tbCapitalService.getUserSometiomeCapital(userId, beginTime, endTime);
        if (userSometiomeCapital == null){
            return Result.error(ResultCode.SERVER_ERROR);
        }else if (userSometiomeCapital.isEmpty()){
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("userSometiomeCapital", userSometiomeCapital);
    }

    /**
     * 添加一个账单
     * @param tbCapital
     * @return
     */
    @ApiOperation(value = "添加一个账单")
    @PostMapping("/addCapital")
    @ResponseBody
    @MyLog("添加一个账单")
    public Result addCapital(@RequestBody TbCapital tbCapital){
        if (tbCapital == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        if (!tbCapitalService.addCapital(tbCapital)) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }



    /**
     * 添加一个账单Vo
     * @param tbCapital
     * @return
     */
    @ApiOperation(value = "添加一个账单Vo")
    @PostMapping("/addCapitalVo")
    @ResponseBody
    @MyLog("添加一个账单Vo")
    public Boolean addCapitalVo(@RequestBody TbCapital tbCapital){
        if (tbCapital == null) {
            return null;
        }
        try {
            return tbCapitalService.addCapital(tbCapital);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }





    /**
     * 根据账单Id删除一个账单
     * @param capitalId
     * @return
     */
    @ApiOperation(value = "根据账单Id删除一个账单")
    @GetMapping("/deleteCapital/{capitalId}")
    @ResponseBody
    @MyLog("根据账单Id删除一个账单")
    public Result deleteCapital(@PathVariable("capitalId") Integer capitalId){
        if (capitalId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        if (!tbCapitalService.deleteCapital(capitalId)) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 根据账单号获取账单
     * @param capitalId
     * @return
     */
    @ApiOperation(value = "根据账单号获取账单")
    @GetMapping("/getUserOneCapital/{capitalId}")
    @ResponseBody
    @MyLog("根据账单号获取账单")
    public Result getUserOneCapital(@PathVariable("capitalId") Integer capitalId){
        if (capitalId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        TbCapital userOneCapital = tbCapitalService.getUserOneCapital(capitalId);
        if (userOneCapital == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok("capital",userOneCapital);
    }
}

